package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.CheckGroup;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 检查组持久层接口
 */
public interface CheckGroupDao {

    /**
     * 分页查询
     * 根据条件查询检查组
     *
     * @param queryString 查询条件
     * @return 检查组集合
     */
    @Select("<script> select * from t_checkgroup <if test=\"queryString !=null and queryString !=''\"> where code like \"%\"#{queryString}\"%\" or name like \"%\"#{queryString}\"%\" or helpCode like \"%\"#{queryString}\"%\" </if></script>  ")
    Page<CheckGroup> findPage(@Param("queryString") String queryString);

    /**
     * 添加检查组   查询自增长主键
     *
     * @param checkGroup 检查组
     */
    //@Options 可以替换成@SelectKey()  @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into t_checkgroup values(null,#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @SelectKey(statement = "select last_insert_id() ", before = false, keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void add(CheckGroup checkGroup);

    /**
     * 添加检查组-检查项中间表
     *
     * @param map 检查组id  检查项id
     */
    @Insert("insert into t_checkgroup_checkitem (checkgroup_id,checkitem_id) values(#{checkgroupId},#{checkitemId})")
    void setCheckGroupAndCheckItem(Map<String, Integer> map);

    /**
     * 根据id查询检查组
     *
     * @param id 检查组id
     * @return 检查组
     */
    @Select("select * from t_checkgroup where id=#{id} ")
    CheckGroup findById(Integer id);

    /**
     * 根据检查组查询检查项id集合
     *
     * @param checkGroupId 检查组id
     * @return 检查项id集合
     */
    @Select("select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{checkGroupId} ")
    List<Integer> findCheckItemIdByCheckGroupId(Integer checkGroupId);

    /**
     * 根据检查组id删除检查组和检查项中间表
     *
     * @param checkGroupId 检查组id
     */
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id=#{checkGroupId} ")
    void deleteAssociation(Integer checkGroupId);

    /**
     * 编辑检查组
     *
     * @param checkGroup 检查组
     */
    @Update("update t_checkgroup set code=#{code},name=#{name},helpCode=#{helpCode},sex=#{sex},remark=#{remark},attention=#{attention} where id=#{id} ")
    void edit(CheckGroup checkGroup);

    /**
     * 根据id删除检查组
     *
     * @param checkGroupId 检查组id
     */
    @Delete("delete from t_checkgroup where id=#{checkGroupId} ")
    void delete(Integer checkGroupId);

    /**
     * 查询所有检查组
     *
     * @return List<CheckGroup> 检查组集合
     */
    @Select("select * from t_checkgroup ")
    List<CheckGroup> findAll();

    /**
     * 根据检查组id 查询套餐-检查组中间表, 得到套餐的数量
     *
     * @param checkGroupId 检查组id
     * @return 套餐使用检查组的次数
     */
    @Select("select count(1) from t_setmeal_checkgroup where checkgroup_id=#{checkGroupId}")
    long findCountByCheckGroupId(Integer checkGroupId);

    /**
     * 通过套餐id 查询所有的检查组  关联查询相应的检查项
     *
     * @param setmealId 套餐id
     * @return List<CheckGroup> 查询关联的检查组
     */
    @Select("select * from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id =#{setmealId})")
    @Results({
            @Result(property = "id", column = "id"),
            //1对多延迟加载
            @Result(property = "checkItems", column = "id", many = @Many(select = "com.itheima.dao.CheckItemDao.findCheckItemsByCheckGroupId"))
    })
    List<CheckGroup> findCheckGroupsBySetmealId(Integer setmealId);

}
